/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUIs;

import Configs.Config;
import Controllers.MyTableFormat;
import Controllers.NhanVienController_2;
import Controllers.TaiKhoanController_2;
import com.db4o.ObjectSet;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import models.NhanVien;
import models.TaiKhoan;


public class f_taikhoan extends javax.swing.JFrame {

    private  Config conn;
    final TaiKhoanController_2 tkcontrol;
    private  NhanVien nvdangnhap;
    private  NhanVienController_2 nvc;
    private  MyTableFormat myFormat;
    public TaiKhoanController_2 gettkcontrol(){
        return this.tkcontrol;
    }
    
    public MyTableFormat getMyFormat(){
        return this.myFormat;
    }
    public javax.swing.JTable getTbtk(){
        return this.tbdstk;
    }
    public f_taikhoan(Config conn, String msnv) {
        this.conn = conn;
        this.tkcontrol = new TaiKhoanController_2(this.conn);
        this.nvc    =    new NhanVienController_2(conn);
        this.nvdangnhap = this.nvc.getWithMSNV(msnv);
        myFormat = new MyTableFormat();
        initComponents();
        tbdstk.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        TableColumn column = null;
        column = tbdstk.getColumnModel().getColumn(0);
        column.setPreferredWidth(15); 
        tbdstk.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        this.tkcontrol.showAllTaiKhoan(tbdstk);
        
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupmenu = new javax.swing.JPopupMenu();
        itemsua = new javax.swing.JMenuItem();
        itemxoa = new javax.swing.JMenuItem();
        pnchinh = new javax.swing.JPanel();
        tftim = new javax.swing.JTextField();
        btntim = new javax.swing.JButton();
        btnlammoi = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbdstk = new javax.swing.JTable();
        btnthem = new javax.swing.JButton();
        btntaikhoan_xoa = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        itemsua.setText("Sửa thông tin tài khoản");
        itemsua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemsuaActionPerformed(evt);
            }
        });
        popupmenu.add(itemsua);

        itemxoa.setText("Xóa tài khoản");
        itemxoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemxoaActionPerformed(evt);
            }
        });
        popupmenu.add(itemxoa);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Quản lý tài khoản");
        setResizable(false);

        btntim.setText("Tìm");
        btntim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btntimActionPerformed(evt);
            }
        });

        btnlammoi.setText("Làm Mới");
        btnlammoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlammoiActionPerformed(evt);
            }
        });

        tbdstk.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Tài Khoản", "Nhân Viên"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tbdstk.setComponentPopupMenu(popupmenu);
        jScrollPane1.setViewportView(tbdstk);

        btnthem.setText("Thêm tài khoản");
        btnthem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthemActionPerformed(evt);
            }
        });

        btntaikhoan_xoa.setText("Tài khoản đã xóa");
        btntaikhoan_xoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btntaikhoan_xoaActionPerformed(evt);
            }
        });

        jButton1.setText("Thoát");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("QUẢN LÍ TÀI KHOẢN");

        javax.swing.GroupLayout pnchinhLayout = new javax.swing.GroupLayout(pnchinh);
        pnchinh.setLayout(pnchinhLayout);
        pnchinhLayout.setHorizontalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnchinhLayout.createSequentialGroup()
                        .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnchinhLayout.createSequentialGroup()
                                .addGap(60, 60, 60)
                                .addComponent(btnthem, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(28, 28, 28)
                                .addComponent(btnlammoi, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(9, 9, 9)
                                .addGap(18, 18, 18)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnchinhLayout.createSequentialGroup()
                                .addGap(40, 40, 40)
                                .addComponent(tftim, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btntim, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btntaikhoan_xoa, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(pnchinhLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pnchinhLayout.setVerticalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel1)
                .addGap(33, 33, 33)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btntaikhoan_xoa)
                    .addComponent(tftim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btntim))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnthem)
                    .addComponent(btnlammoi)
                    .addComponent(jButton1))
                .addGap(20, 20, 20))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(pnchinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnchinh, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents
       
    private void itemsuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemsuaActionPerformed
       int row = tbdstk.getSelectedRow();
        if (row != -1) {
            String user = tbdstk.getValueAt(row, 1).toString();
            TaiKhoan tk = this.tkcontrol.getWithTenTaiKhoan(user);
            //f_nhanvien_sua f = f_nhanvien_sua.getInstance(nv, this.conn);
            f_taikhoan_sua f_tk_s  = new f_taikhoan_sua(conn,tk,this);
            f_tk_s.setVisible(true);
        }
        tbdstk.clearSelection();
    }//GEN-LAST:event_itemsuaActionPerformed

    private void itemxoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemxoaActionPerformed
        int check = JOptionPane.showConfirmDialog(this, "Bạn có muốn xóa tài khoản này", "Warning", JOptionPane.YES_NO_OPTION);
        if(check==JOptionPane.YES_OPTION){
            int row = tbdstk.getSelectedRow();
            if (row != -1) {
                String user = tbdstk.getValueAt(row, 1).toString();
                TaiKhoan tk = this.tkcontrol.getWithTenTaiKhoan(user);
                this.tkcontrol.deleteTaiKhoan(tk, tk.getMSNV());
                JOptionPane.showMessageDialog(this, "Đã xóa Tài khoản Thành Công!");
                this.getMyFormat().clearTable(this.getTbtk());
                tkcontrol.showAllTaiKhoan(this.getTbtk());
            }
            tbdstk.clearSelection(); 
        }
    }//GEN-LAST:event_itemxoaActionPerformed

    private void btntaikhoan_xoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btntaikhoan_xoaActionPerformed
        this.myFormat.clearTable(tbdstk);
        this.tkcontrol.showAllTaiKhoan_isDelete(tbdstk);
    }//GEN-LAST:event_btntaikhoan_xoaActionPerformed

    private void btnthemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthemActionPerformed
        f_taikhoan_them f = f_taikhoan_them.getInstance(this.conn,this);
        f.setVisible(true);
    }//GEN-LAST:event_btnthemActionPerformed

    private void btnlammoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlammoiActionPerformed
        this.myFormat.clearTable(tbdstk);
        tkcontrol.showAllTaiKhoan(tbdstk);
    }//GEN-LAST:event_btnlammoiActionPerformed

    private void btntimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btntimActionPerformed
        String user = tftim.getText();
        ObjectSet<TaiKhoan> tks = this.tkcontrol.getWithTenTaiKhoan_Gandung(user);
        if(!tks.isEmpty()){
            this.myFormat.clearTable(this.tbdstk);
            tkcontrol.showTimkiem(tbdstk, tks);
        }
        tftim.setText("");
    }//GEN-LAST:event_btntimActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnlammoi;
    private javax.swing.JButton btntaikhoan_xoa;
    private javax.swing.JButton btnthem;
    private javax.swing.JButton btntim;
    private javax.swing.JMenuItem itemsua;
    private javax.swing.JMenuItem itemxoa;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnchinh;
    private javax.swing.JPopupMenu popupmenu;
    private javax.swing.JTable tbdstk;
    private javax.swing.JTextField tftim;
    // End of variables declaration//GEN-END:variables
}
